<template>
  <div>
    <title-panel />
    <van-form
      input-align="right"
      error-message-align="right"
      :show-error-message="false"
      show-error
      scroll-to-error
      validate-first
      @failed="onFailed"
      @submit="onSubmit"
    >
      <van-cell class="btnfrom">
        <van-field
          class="fromInput"
          readonly
          name="count"
          label="耳标号"
          :value="obj.count"
          required
          is-link
          label-width="35%"
          placeholder="请选择"
          input-align="right"
          :rules="[{ required: true, message: '请选择耳标号' }]"
          @click="goFodder"
        />
        <van-button native-type="button" plain color="#37B5B5" @click="scanClick()" class="addbtn" type="primary"
          >扫一扫</van-button
        >
      </van-cell>

      <van-field readonly v-model="obj.preventionNo" name="obj" label="畜禽标识编号" required input-align="right" />
      <van-field
        readonly
        :value="obj.buyDate"
        name="buyDate"
        label="产羔日期"
        placeholder="请选择产羔日期"
        input-align="right"
        required
        is-link
        @click="showPicker1 = true"
        :rules="[{ required: true, message: '请选择产羔日期' }]"
      />
      <van-popup v-if="!isDisabled" v-model="showPicker1" position="bottom">
        <van-datetime-picker
          v-model="currentDate"
          type="date"
          @confirm="onConfirmStartDate"
          @cancel="showPicker1 = false"
        />
      </van-popup>

      <van-field
        required
        input-align="right"
        v-model="obj.femaleNumber"
        name="femaleNumber"
        label="母羔数量"
        type="digit"
        placeholder="请输入"
        :rules="[{ required: true, message: '请输入母羔数量' }]"
      />

      <van-field
        required
        input-align="right"
        v-model="obj.maleNumber"
        name="maleNumber"
        label="公羔数量"
        type="digit"
        placeholder="请输入"
        :rules="[{ required: true, message: '请输入公羔数量' }]"
      />

      <van-field
        required
        input-align="right"
        v-model="obj.weakLambNum"
        name="weakLambNum"
        label="弱羔数量"
        type="digit"
        placeholder="请输入"
        :rules="[{ required: true, message: '请输入弱羔数量' }]"
      />
      <van-field
        required
        input-align="right"
        v-model="obj.stillbirthNum"
        name="stillbirthNum"
        label="死胎数"
        type="digit"
        placeholder="请输入"
        :rules="[{ required: true, message: '请输入死胎数' }]"
      />
      <van-field
        required
        input-align="right"
        v-model="obj.robustFemaleLambNum"
        name="robustFemaleLambNum"
        label="健壮母羔数量"
        type="digit"
        placeholder="请输入"
        :rules="[{ required: true, message: '请选输健壮母羔数量' }]"
      />

      <van-field
        required
        input-align="right"
        v-model="obj.robustMaleLambNum"
        name="robustMaleLambNum"
        label="健壮公羔数量"
        placeholder="请输入"
        :rules="[{ required: true, message: '请输入健壮公羔数量' }]"
      />

      <van-popup v-model="natureShows" position="bottom">
        <van-picker
          show-toolbar
          :columns="natureNameList"
          value-key="name"
          @confirm="onConfirmType"
          @cancel="natureShows = false"
        />
           
      </van-popup>
      <div class="spacing" />
      <div class="add-btn">
        <van-button
          block
          type="info"
          color="#37B5B5"
          style="width: 85%; margin: auto; margin-top: 50px"
          native-type="submit"
          size="large"
          :loading="loading"
          loading-text="提交中..."
          >保存</van-button
        >
      </div>
    </van-form>
  </div>
</template>
<script>
import {
  Form,
  Field,
  Calendar,
  RadioGroup,
  Radio,
  Dialog,
  Toast,
  DatetimePicker,
  Picker,
  Button,
  Icon,
  Popup,
  Cell
} from 'vant'
import UploadImg from '@/components/uploadImg'
import TitlePanel from '@/components/titlePanel'
import dayjs from 'dayjs'
import { getValue, setValue, clearValue } from '@/utils/auth'
import { getHandle, postHandle } from '../../api'
export default {
  components: {
    UploadImg,
    TitlePanel,
    [Form.name]: Form,
    [Field.name]: Field,
    [Calendar.name]: Calendar,
    [RadioGroup.name]: RadioGroup,
    [Radio.name]: Radio,
    [Picker.name]: Picker,
    [DatetimePicker.name]: DatetimePicker,
    [Button.name]: Button,
    [Icon.name]: Icon,
    [Popup.name]: Popup,
    [Toast.name]: Toast,
    [Cell.name]: Cell,
    [Dialog.Component.name]: Dialog.Component
  },
  data() {
    return {
      contentList: '',
      fileInfoList: [],
      currentDate: new Date(),
      showPicker1: false,
      loading: false,
      flag: getValue('swBj') || '',
      //   typeList: [],
      //   resultList: [],
      isDisabled: false,
      natureShows: false,
      natureNameList: [0, 1, 2, 3, 4],
      siteList: [],
      natureShow: false,
      natureNameLists: [],
      fertiliList: [],
      houseInfoId: getValue('fzmHourseId') || '',
      obj: {
        buyDate: dayjs(new Date()).format('YYYY-MM-DD'),
        count: '',
        femaleNumber: '',
        maleNumber: '',
        weakLambNum: '',
        stillbirthNum: '',
        robustMaleLambNum: '',
        robustFemaleLambNum: '',

        lambingInfoIds: {}
      },
      contentObj: {},
      type: null
    }
  },
  created() {},
  mounted() {
    this.$bridge.register('backScanData', data => {
      if (JSON.parse(data).individualId) {
        let obj = {
          earLabelNo: JSON.parse(data).earLabelNo,
          id: JSON.parse(data).individualId,
          preventionNo: JSON.parse(data).preventionNo
        }
        setValue('earTag2d', JSON.stringify(obj))
        this.obj.count = JSON.parse(data).earLabelNo
        this.obj.preventionNo = JSON.parse(data).preventionNo

        this.obj.lambingInfoIds = JSON.parse(data).individualId
      } else {
        Toast('羊只信息未录入')
      }
    })
    this.$bridge.register('refresh', data => {
      if (!!getValue('cgObj')) {
        this.obj = JSON.parse(getValue('cgObj'))
      }
      if (!!getValue('earTag2d')) {
        this.obj.lambingInfoIds = JSON.parse(getValue('earTag2d')).id
        this.obj.count = JSON.parse(getValue('earTag2d')).earLabelNo
                this.obj.preventionNo = JSON.parse(getValue('earTag2d')).preventionNo

      }
    })
  },
  methods: {
    scanClick() {
      this.$bridge.call('base.scanOneScan')
    },

    openChoose(str) {
      this.type = str
      this.natureShows = true
    },
    onConfirmType(value) {
      if (this.type == 'a') {
        this.obj.femaleNumber = value
      } else if (this.type == 'b') {
        this.obj.femaleLifeNumber = value
      } else if (this.type == 'c') {
        this.obj.maleNumber = value
      } else if (this.type == 'd') {
        this.obj.maleLeftNumber = value
      }

      // this.changeCode = value.code
      this.natureShows = false
    },
    goFodder() {
      setValue('fzmSex', '母')
      setValue('cgObj', JSON.stringify(this.obj))
      this.$bridge.call('addCheckEarTag2d')
    },
    //获取默认数据
    initialize(id) {},
    //开始时间
    onConfirmStartDate(value) {
      this.obj.buyDate = dayjs(value).format('YYYY-MM-DD')
      this.showPicker1 = false
    },
    onFailed(errorInfo) {
      Toast(errorInfo.errors[0].message)
    },
    onSubmit(value) {
      this.loading = true
      console.log(value)

      let params = {
        houseInfoId: this.houseInfoId,
        lambDate: value.buyDate,
        individualInfoId: this.obj.lambingInfoIds,
        femaleLambNum: value.femaleNumber,
        maleLambNum: value.maleNumber,
        weakLambNum: value.weakLambNum,
        stillbirthNum: value.stillbirthNum,
        robustFemaleLambNum: value.robustFemaleLambNum,
        robustMaleLambNum: value.robustMaleLambNum,
        subjectInfoId: getValue('subjectInfoId'),
        tenantCode: getValue('tenantCode')
      }
      postHandle('/agri/api/yzbzh/lambingRecord/save', params).then(res => {
        if (res.status == '0') {
          this.$toast(res.msg)
          clearValue('cgObj')
          clearValue('earTag2d')
          clearValue('fzmSex')
          this.$bridge.call('base.closePage')
        } else {
          Toast.fail(res.msg | '操作失败')
        }
        setTimeout(() => {
          this.loading = false
        }, 1000)
      })
      // let imgInfos = this.obj.imgInfos.map((item) => item.url).join(",");
    }
  }
}
</script>
<style lang="scss" scoped>
.tip {
  padding-top: 15px;
  color: #999999;
  font-size: 12px;
  line-height: 16px;
  margin: auto;
}
.btn {
  width: 80%;
  margin: 50px auto 0 auto;
}
.btn > div {
  height: 50px;
  background: #2871fe;
  border-radius: 4px;
  text-align: center;
  line-height: 50px;
  color: #ffffff;
  font-size: 17px;
}
.btn .cancel {
  margin-top: 20px;
  background: rgba(40, 113, 254, 0.1);
  border: 1px solid #2871fe;
  color: #2871fe;
}
::v-deep .van-field__label {
  width: 6em;
}
.bthStyle {
  width: 56px;
  height: 20px;
  border-radius: 10px;
  border: 1px solid #2871fe;
}
.active {
  background-color: #fff;
  color: #2871fe;
}
.add-btn {
  padding-bottom: 20px;
}
.addbtn {
  width: 50px;
  height: 24px;
  font-size: 13px;
  padding: 0;
  margin-right: 13px;
  z-index: 10;
  float: right;
  margin-top: -38.5px;
}
.btnfrom {
  padding: 0;
}
.fromInput {
  width: 86%;
}
</style>
